<?php
Class Manager_Model_newssolution extends Zend_Db_Table
{	
	protected $_name ="article_solution";
	protected $_primary ="id";
	
	public function list_news()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'news'));		          
        $results = $db->fetchAll($select);
        return $results;
	}
	public function sec_id()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'sections_solution'),array('title','id'));		          
        $results = $db->fetchPairs($select);
        return $results;
	}
	public function cat_id()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'category_solution'),array('title','id'))
		          ;		          
        $results = $db->fetchAll($select);
        return $results;
	}
	public function listsec()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'sections_solution'),array('id','title'));		          
        $results = $db->fetchPairs($select);
        return $results;
	}
	public function listcat()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'category_solution'),array('id','title'));		          
        $results = $db->fetchPairs($select);
        return $results;
	}
	
	public function searchkq($keyword,$status=-1,$catnews=-1,$secnews=-1)
	{
		$db = Zend_Registry::get('connectDb');
			$query="SELECT DISTINCT a.* ,c.title as name_category,s.title as theloai,l.title as ngonngu 
                    FROM  sections_solution as s INNER JOIN (category_solution as c INNER JOIN (article_solution as a INNER JOIN language as l ON a.language_id=l.id) ON a.cat_id=c.id)ON a.section_id=s.id
					WHERE (a.published=$status or $status=-1) AND (a.title like '%$keyword%') AND (a.cat_id=$catnews or $catnews=-1)AND(a.section_id=$secnews or $secnews=-1)
					ORDER BY a.id desc";
			$result = $db->fetchAll($query)	;
		return $result;	
	}
	
  public  function listItemabout($status=-1,$catnews=-1,$secnews=-1)
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT DISTINCT a.* ,c.title as name_category,s.title as theloai,l.title as ngonngu 
                    FROM  sections_solution as s INNER JOIN (category_solution as c INNER JOIN (article_solution as a INNER JOIN language as l ON a.language_id=l.id) ON a.cat_id=c.id)ON a.section_id=s.id
					WHERE ((a.published=$status or $status=-1)AND(a.cat_id=$catnews or $catnews=-1) AND (a.section_id=$secnews or $secnews=-1) )
					ORDER BY a.id desc";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
	
	public function delete_muti($arrParam = null)
	{
	$tblGroup = new Manager_Model_newssolution();
		if(count($arrParam['cid'])>0)
	 	{
			$ids = implode(',', $arrParam['cid']);
			$where = 'id IN (' . $ids . ')'; 
			$tblGroup->delete($where); 
			 
	 	}
	}
	
	public function delete_data($id)
	{
		  	$tblGroup = new Manager_Model_newssolution();
			$where = "id =$id";
		    $tblGroup->delete($where);  	
	}
	
	public function getrow($id)
	{
				$db = Zend_Registry::get('connectDb');
				$query="Select * from article_solution  where id=$id";
				$result = $db->fetchAll($query);
				return $result;
	}
	
   public function save($data)
	{	
    
	  if ($data['key']=='add')
		  {	
			$db = Zend_Registry::get('connectDb');
			$introtext = stripslashes($data['introtext']);
		   	$fulltext = stripslashes($data['fulltext']);
		  
		  	$data1 = array(
			              	'title'         	=> $data['title'],
		  	                'introtext'	        => $introtext,
		  	                'images'	        => $data['images'],
		  	                'fulltext'	        => $fulltext,
							'section_id'		=> $data['section_id'],
		  					'cat_id'			=> $data['cat_sub'],
		  					'published'		    => $data['published'],
		  	                'featured'		    => $data['featured'],
		  	                'special'		    => $data['special'],
							'order'			    => $data['order'],
	                      	'language_id'       => $data['language_id'],
		  					
		  			
						 );
	        $db->insert('article_solution', $data1);
		  }
	else 
		  {
		  $tblGroup = new Manager_Model_newssolution();
		  $where ='id ='.$data['id'];
    	  $introtext = stripslashes($data['introtext']);
		  $fulltext = stripslashes($data['fulltext']);
		 
		  $data1 = array(
			              	'title'         	=> $data['title'],
		  	                'introtext'	        => $introtext,
		  	                'images'	        => $data['images'],
		  	                'fulltext'	        => $fulltext,
							'section_id'		=> $data['section_id'],
		  					'cat_id'			=> $data['cat_sub'],
		  					'published'		    => $data['published'],
		  	                'featured'		    => $data['featured'],
		  	                'special'		    => $data['special'],
							'order'			    => $data['order'],
	                      	'language_id'       => $data['language_id'],
		  					
		  			
						 );
    	  $tblGroup->update($data1,$where);
		  
		  }  
     }
     
   public function cat_id_cat($id)
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'category_solution'),array('id','title'))
		          ->where('section_id = ?',$id);
		$results = $db->fetchPairs($select);
        return $results;
	}
	
   public function listproduct()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('dt' => 'details_product'),array('id','title'))
		          ->where('published = ?','1');;		          
        $results = $db->fetchAll($select);
        return $results;
	}
   
   public function listproduct_activiti($id)
	{
		$db = Zend_Registry::get('connectDb');
        $select = "SELECT dt.id,dt.title
                   FROM solution_product as st INNER JOIN details_product as dt ON st.product=dt.id
                   WHERE st.solution=$id";		          
        $results = $db->fetchAll($select);
        return $results;
	}
	
    public function layid()
	{
		 		$db = Zend_Registry::get('connectDb');	
				$query="Select * from article_solution order by id DESC LIMIT 0,1";
		  		$result = $db->fetchRow($query);
		   		$id=$result[id]; 
		   		return $id;
	}
	
    public function save_itemt($data,$id)
	{
		 		$db         = Zend_Registry::get('connectDb');	
		 		$array      = $data['toBox'];
				if (count($array)>0)
				{
		 		   for($i=0;$i<count($array);$i++)  
		 		   {
		 		   	$data1 = array(
			              	'solution'         	=> $id,
		  	                'product'	        => $array[$i],
		  	             );
	                  $db->insert('solution_product', $data1);
		 		   }
				}
		   	
	}
    public function delete_item($id)
	{
		$db = Zend_Registry::get('connectDb');
		$where='solution='.$id;	
		$db->delete('solution_product',$where);
	}
	
}